package com.me.mygdxgame.player;

import kryonetpackets.NetworkPlayer;
import kryonetpackets.RGB;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;

public class Player extends Actor{
	
	private static final int SPRITE_DIM = 128;
	private static final int SPRITE_WALK_ROW = 0;

	private String location;
	private PlayerGroup playerGroup;

	private String name;
	private Animation animation;
	private Texture spriteSheet;
	private TextureRegion[][] spriteFrames;
	private SpriteBatch spriteBatch;
	private RGB rgbcolor;
	private RGB currentColor;
	
	private float time = 0f;
	private float xVelocity = 0f;
	
	
	
	public Player(NetworkPlayer p) {
		//TODO: setup, including appearance, location, and playerGroup
		this.spriteSheet = new Texture(Gdx.files.internal(
				"data/textures/TestPlayerGraphics.png"));
		this.spriteFrames = TextureRegion.split(spriteSheet,
				SPRITE_DIM, SPRITE_DIM);
		this.animation = new Animation(0.4f, spriteFrames[SPRITE_WALK_ROW]);
		this.animation.setPlayMode(Animation.LOOP_PINGPONG);
		this.spriteBatch = new SpriteBatch();
		
		if (p != null) {
			this.name = p.getName();
			this.rgbcolor = p.getColor();
			this.currentColor = p.getColor();
			
			this.setCurrentColor(this.currentColor);
		}
	}
	
	//This is where we add
	public void initPlayer()
	{
		this.addListener(new InputListener() {
			public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
			{
				//System.out.println("Clicked a player!");
				
				return false;
			}
			public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor)
			{
				//System.out.println("Mouse entered a player!");
				setCurrentColor(new RGB(0,0,0));
			}
			public void exit(InputEvent event, float x, float y, int pointer, Actor fromActor)
			{
				//System.out.println("Moused exited a player!");
				setCurrentColor(getRGBColor());
			}
		});
		
	}
	
	public String getName() { return name; }
	
	public void setName(String name) { this.name = name; }
	
	/**
	 * This should only be called by PlayerGroup.addPlayer()
	 * @param pg
	 */
	public void setGroup(PlayerGroup pg) { this.playerGroup = pg; }
	
	public PlayerGroup getGroup() { return this.playerGroup; }
	
	/**
	 * Changes the location of this Player. This should be the names of the
	 * planet or building they are currently in.
	 * @param loc
	 */
	public void changeLocation(String loc) {this.location = loc;}
	
	/**
	 * Checks to see if the player is in the provided location.
	 * @param loc
	 * @return
	 */
	public boolean isInLocation(String loc) {
		return this.location.equals(loc);
	}
	
	/**
	 * This function should only be called by PlayerGroup's dispose()
	 */
	public void dispose() {
		this.spriteSheet.dispose();
		this.spriteBatch.dispose();
	}
	
	/**
	 * Render's this Player, should only be called by the Player's
	 * PlayerGroup.render()
	 * @param delta
	 */
	public void render(float delta, float cameraXVelocity) {
		// Apply negative camera velocity to position
		this.setX(this.getX() - (cameraXVelocity * delta));
		// Apply this unit's velocity to itself
		this.setX(this.getX() + (this.xVelocity * delta));
		// Update the Actor bounds
		this.setBounds(this.getX(), this.getY(), SPRITE_DIM, SPRITE_DIM);
		
		this.time += (delta * (this.xVelocity / 50f));
		this.spriteBatch.begin();
		//TODO Get the correct animation, and facing the correct way
		if (this.xVelocity == 0f) {
			this.spriteBatch.draw(animation.getKeyFrame(0f, false),
					this.getX(), this.getY());
		}
		else {
			this.spriteBatch.draw(animation.getKeyFrame(this.time, true),
					this.getX(), this.getY());
		}
		this.spriteBatch.end();
	}

	public void setXVelocity(float xVel) { this.xVelocity = xVel; }
	
	public void setCurrentColor(RGB ncolor)
	{
		
		this.currentColor = ncolor;
		this.spriteBatch.setColor(new Color(ncolor.getR(),ncolor.getG(),ncolor.getB(),1));
		
	}
	public RGB getCurrentRGBColor() {
		
		return currentColor;
	}
	public void setRGBColor(RGB ncolor) {
		
		this.rgbcolor = ncolor;
	}
	public RGB getRGBColor() {
		
		return rgbcolor;
	}
	
	public boolean equals(Player p) {
		return p.getName().equals(this.getName());
	}
}
